<template>
	<view class="paymoney">
		<view class="paymoney-title">
			<text class="paymoney-title-img"></text>
			<text class="paymoney-title-txt font_blod">充值金额</text>
		</view>
		<view class="paymoney-paybox">
			<text class="paymoney-paybox-txt">￥</text>
			<input class="paymoney-paybox-input" v-model="money"placeholder-class="inputplace" placeholder="0" type="number" value="" />
			<image class="paymoney-paybox-close" src=".https://www.zzzsyh.com/applets/agent/agent/close_two.png" mode=""></image>
		</view>
		<view class="font_blod">
			<view v-if="show_btn" class="btn" @click="cli_pay">
				确定支付
			</view>
			<view v-else class="btn btn-false" @click="cli_pay">
				确定支付
			</view>
		</view>
		
	</view>
</template>

<script>
	import allapi from '../api/index.js'
	export default {
		data() {
			return {
				factoryId: uni.getStorageSync('id_factory'),
				money: '',
			}
		},
		computed: {
			show_btn () {
				let monry = this.money
				if (monry) {
					return true
				}else {
					return false
				}
			}
		},
		methods: {
			cli_pay () {
				let _that = this
				let money = _that.money
				if (money) {
					_that.zhifu()
				}else {
					uni.showToast({
						title: '请输入充值金额',
						icon: 'none'
					})
				}
				
			},
			zhifu () {
				let _that = this
				wx.login({
				  success (res) {
					  console.log(res);
				    if (res.code) {
				      //发起网络请求
				      wx.request({
				        url: allapi.getWxOpenId,
				        data: {
				          code: res.code
				        },
						success: (res) => {
							console.log(2222,res);
							let openid = res.data.data.openid
							uni.request({
								url: allapi.wxPayAppletsFactoryRecharge,
								data: {
									factoryId: _that.factoryId,
									openId: openid,
									totalfee: _that.money
								},
								success: (res) => {
									console.log(3333, res);
									let dat = res.data.data[0]
									wx.requestPayment({
									  timeStamp: dat.timeStamp,
									  nonceStr: dat.nonceStr,
									  package: dat.package,
									  signType: 'MD5',
									  paySign: dat.paySign,
									  success (res) {
										  console.log('成功',res);
										  uni.showToast({
										  	title: res.data.msg,
											icon: 'none'
										  })
										  _that.money = ''
									  },
									  fail (res) {
										  console.log('失败', res);
										  uni.showToast({
										  	title: res.data.msg,
										  											icon: 'none'
										  })
									  }
									})
								},fail: (err) => {
									console.log(4444,err);
									uni.showToast({
										title: res.data.msg,
																				icon: 'none'
									})
								}
							})
						}
				      })
				    } else {
				      console.log('登录失败！' + res.errMsg)
				    }
				  }
				})
			}
		}
	}
</script>

<style scoped>
	
	.paymoney-title {
		display: flex;
		align-items: center;
		margin-left: 24rpx;
		margin-top: 32rpx;
	}
	.paymoney-title-img {
		width: 10rpx;
		height: 34rpx;
		background: #5DD4D6;
		border-radius: 5rpx;
	}
	.paymoney-title-txt {		
		font-size: 32rpx;
		color: #333333;
		margin-left: 24rpx;
	}
	.paymoney-paybox {
		margin: 32rpx;
		height: 224rpx;
		background: #FFFFFF;
		border-radius: 24rpx;
		display: flex;
		align-items: center;
		padding: 0 24rpx;
		font-size: 72rpx;
	}
	.paymoney-paybox-txt {
		
	}
	.paymoney-paybox-input {
		flex: 1;
		font-size: 72rpx;
		height: 118rpx;
	}
	.paymoney-paybox-close {
		width: 40rpx;
		height: 40rpx;
	}
	.btn {
		padding: 25rpx 10rpx;
		background: #5DD4D6;
		text-align: center;
		color: #FFFFFF;
		margin: 0 40rpx;
		font-size: 36rpx;
		border-radius: 16rpx;
	}
	.btn-false {
		opacity: 0.5;
	}
</style>
